/* 
 * Advanced Foundation Classes
 * Copyright (C) 2000/2004  Fabio Rotondo - Andrea Galimberti
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *  
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "window.h"

static const char class_name[] = "EGUI Widget Window";

static int afc_easygui_internal_window_get_template ( DynamicClass * dyn );
static int afc_easygui_internal_cb_window ( DynamicClass * dyn );

static int afc_easygui_window_method_add ( DynamicClass * dc );
static int afc_easygui_window_method_set_title ( DynamicClass * dc );

static gint afc_easygui_window_event_delete ( GtkWidget * w, GdkEvent * e, gpointer data );
static void afc_easygui_window_event_destroy ( GtkWidget * w, gpointer data );
//static gboolean afc_easygui_window_event_activate ( GtkWidget * w, GdkEventFocus * event, gpointer data );
static gboolean afc_easygui_window_event_activate ( GtkWidget * w, gpointer data );
static gboolean afc_easygui_window_event_deactivate ( GtkWidget * w, GdkEventFocus * event, gpointer data );

// static EasyGUISignalFunc afc_easygui_window_internal_get_sig ( DynamicClass * dc, char * name );

#define EVENT_ACTIVATE "focus-in-event"
#define EVENT_DEACTIVATE "focus-out-event"

DynamicClass * afc_easygui_internal_window_new_instance ()
{
	DynamicClass * dyn = NULL;

	if ( ( dyn = afc_dynamic_class_new () ) == NULL ) {
		AFC_LOG_FAST_INFO ( AFC_ERR_NO_MEMORY, "dynamic class: window" );
		return ( NULL );
	}

	afc_dynamic_class_add_method ( dyn, "get_template", NULL,  	afc_easygui_internal_window_get_template );
	afc_dynamic_class_add_method ( dyn, "open_callback", NULL,  	afc_easygui_internal_cb_window );
	afc_dynamic_class_add_method ( dyn, "add", NULL,  		afc_easygui_window_method_add );
	afc_dynamic_class_add_method ( dyn, "enabled",		NULL,	afc_easygui_widget_method_enabled );

	afc_dynamic_class_add_method ( dyn, "set_title", 	NULL,	afc_easygui_window_method_set_title );

	return ( dyn );
}

int afc_easygui_internal_window_del_instance ( DynamicClass * dyn )
{
	return ( afc_easygui_widget_standard_free ( dyn ) );
}

static int afc_easygui_internal_window_get_template ( DynamicClass * dyn )
{
	dyn->result = "TITLE DELETE/K DESTROY/K ACTIVATE/K DEACTIVATE/K BORDERLESS/S MAINWIN/S";
	return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_internal_cb_window ( DynamicClass * dyn )
{
	EasyGUI * egui = dyn->info;
	EasyGUIContext * context = egui->curr_context;
	char * title = NULL;
	char * delete, * destroy, * activate, * deactivate;
	short  main_win, borderless;
	GtkWidget * win;
	DynamicClass * dc;
	int afc_res;

	// printf ( "Win Context: %x\n", ( int ) context );

	afc_easygui_widget_parse_default_params ( egui );

	delete 		= afc_cmd_parser_arg_get_by_name ( egui->parser, "DELETE" );
	destroy 	= afc_cmd_parser_arg_get_by_name ( egui->parser, "DESTROY" );
	activate 	= afc_cmd_parser_arg_get_by_name ( egui->parser, "ACTIVATE" );
	deactivate 	= afc_cmd_parser_arg_get_by_name ( egui->parser, "DEACTIVATE" );

	main_win	= ( short ) ( int ) afc_cmd_parser_arg_get_by_name ( egui->parser, "MAINWIN" );
	borderless	= ( short ) ( int ) afc_cmd_parser_arg_get_by_name ( egui->parser, "BORDERLESS" );

	if ( ( dc = afc_dynamic_class_master_new_instance ( egui->commands, "window" ) ) == NULL )
		return ( AFC_LOG_FAST_INFO ( AFC_ERR_NO_MEMORY, "window instance" ) );

	// fprintf ( stderr, "DC: %x\n", ( int ) dc );

	WIDGET_SET_CONTEXT ( dc, context );

	win = gtk_window_new ( GTK_WINDOW_TOPLEVEL );

	context->win = win;
	context->main_win = main_win;
	
	if ( ( title = afc_cmd_parser_arg_get_by_name ( egui->parser, "TITLE" ) ) != NULL )
		gtk_window_set_title ( GTK_WINDOW ( win ), title );

	afc_easygui_context_add_container ( context, win, dc, AFC_EASYGUI_CONTAINER_TYPE_WINDOW, FALSE );

	afc_easygui_widget_add_default_events ( context, dc, win );

	/* the two following functions provide default behaviour if not otherwise specified.
	   The default behaviour is to close the window and quit GTK when the user presses the X widget
	   on the window's title bar */

	if ( delete )
	{
		if ( ( afc_res = afc_easygui_widget_connect_signal ( dc, win, "delete_event", delete, NULL, (GtkSignalFunc) afc_easygui_window_event_delete ) ) != AFC_ERR_NO_ERROR )
			return ( afc_res );
	} else 
		g_signal_connect ( win, "delete_event", G_CALLBACK ( afc_easygui_window_event_delete ), dc );

	if ( destroy )
	{
		if ( ( afc_res = afc_easygui_widget_connect_signal ( dc, win, "destroy", destroy, NULL, (GtkSignalFunc) afc_easygui_window_event_destroy ) ) != AFC_ERR_NO_ERROR )
			return ( afc_res );
	} else 
		g_signal_connect ( win, "destroy", G_CALLBACK ( afc_easygui_window_event_destroy ), dc );

	if ( activate )
	{
		if ( ( afc_res = afc_easygui_widget_connect_signal ( dc, win, EVENT_ACTIVATE, activate, NULL, (GtkSignalFunc) afc_easygui_window_event_activate ) ) != AFC_ERR_NO_ERROR )
			return ( afc_res );

	}

	if ( deactivate )
	{
		if ( ( afc_res = afc_easygui_widget_connect_signal ( dc, win, "focus-out-event", deactivate, NULL, (GtkSignalFunc) afc_easygui_window_event_deactivate ) ) != AFC_ERR_NO_ERROR )
			return ( afc_res );
	}

	// the following instruction allocates the X window's resources before a show() is called: this is necessary
	// for example if some pixmaps must be created in the window. 
	gtk_widget_realize ( win );

	// If borderless, we remove decorations
	if ( borderless ) gtk_window_set_decorated ( GTK_WINDOW ( win ), FALSE );

	return ( AFC_ERR_NO_ERROR );	
}

// EVENTS
static gint afc_easygui_window_event_delete ( GtkWidget * w, GdkEvent * e, gpointer data )
{
	DynamicClass * dc = data;
	EasyGUISignalFunc sig;

	WIDGET_CALL_CALLBACK ( dc, sig, "destroy" );

	return ( FALSE );
}

static void afc_easygui_window_event_destroy ( GtkWidget * w, gpointer data )
{
	DynamicClass * dc = data;
	EasyGUIContext * context = WIDGET_GET_CONTEXT ( dc );
	EasyGUISignalFunc sig;

	WIDGET_CALL_CALLBACK ( dc, sig, "destroy" );

	if ( context->main_win )
		gtk_main_quit ();

	afc_easygui_context_delete ( context );
}

// static gboolean afc_easygui_window_event_activate ( GtkWidget * w, GdkEventFocus * event, gpointer data )
static gboolean afc_easygui_window_event_activate ( GtkWidget * w, gpointer data )
{
	DynamicClass * dc = data;
	EasyGUISignalFunc sig;

	/*
	int activate;

	activate = ( int ) afc_dynamic_class_get_var ( dc, "egui_activate" );
	printf ( "ACTIVATE: %d\n", activate );

	if ( activate == 1 ) return ( TRUE );

	afc_dynamic_class_set_var ( dc, AFC_DYNAMIC_CLASS_VAR_KIND_NUM, "egui_activate", ( void * ) 1 );
	*/

	fprintf ( stderr, "activate focus: %x\n", ( int ) dc  );

	WIDGET_CALL_CALLBACK ( dc, sig, EVENT_ACTIVATE );

	return ( TRUE );
}

static gboolean afc_easygui_window_event_deactivate ( GtkWidget * w, GdkEventFocus * event, gpointer data )
{
	DynamicClass * dc = data;
	EasyGUISignalFunc sig;
	int activate;

	activate = ( int ) afc_dynamic_class_get_var ( dc, "egui_activate" );

	printf ( "DEACTIVATE: %d\n", activate );

	if ( activate == 0 ) return ( TRUE );

	afc_dynamic_class_set_var ( dc, AFC_DYNAMIC_CLASS_VAR_KIND_NUM, "egui_activate", ( void * ) 0 );

	fprintf ( stderr, "deactivate focus: %x\n", ( int ) dc  );

	WIDGET_CALL_CALLBACK ( dc, sig, "focus-out-event" );

	return ( TRUE );
}

// METHODS
static int afc_easygui_window_method_add ( DynamicClass * dc )
{
	GtkWidget * w;
	EasyGUIContext * context;
	int type;
	
	w 	= afc_array_master_first ( dc->args );
	type	= ( int ) afc_array_master_next ( dc->args );

	if ( type == AFC_EASYGUI_CONTAINER_TYPE_MENU ) return ( AFC_ERR_NO_ERROR );

	context = WIDGET_GET_CONTEXT(dc);

	gtk_container_add ( GTK_CONTAINER ( context->win ), w );
	
	return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_window_method_set_title ( DynamicClass * dc )
{
	EasyGUIWidgetData * ewidget;
	char * t;
	
	WIDGET_GET_EWIDGET ( dc, ewidget );
		
	t 	= afc_array_master_first ( dc->args );

	if ( t != NULL ) gtk_window_set_title ( GTK_WINDOW ( ewidget->gtk_handle ), t );

	return ( AFC_ERR_NO_ERROR );
}


// INTERNAL FUNCTIONS
/*
static EasyGUISignalFunc afc_easygui_window_internal_get_sig ( DynamicClass * dc, char * name )
{
	if ( dc->private_vars == NULL ) return ( NULL );

	return ( ( EasyGUISignalFunc ) afc_dictionary_get ( dc->private_vars, name ) );
}
*/
